#include <iostream>
#include <vector>
#include "listnode.h"
using namespace std;

class Solution {
public:
    ListNode* FindKthToTail(ListNode* pListHead, unsigned int k) {
        ListNode *p = pListHead;
        int length = 0;
        while (p != nullptr) {
            length++;
            p = p->next;
        }
        length -= k;
        p = pListHead;
        for (int index = 0; index < length; index++) {
            p = p->next;
        }
        return length < 0 ? nullptr : p;
    }
};

int main() {
    cout << Solution().FindKthToTail(buildList({1, 2, 3, 4, 5, 6}), 3)->val << endl;
    return 0;
}